package com.example.foo.product.service;

import java.time.LocalDateTime;
import java.util.List;

import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.product.model.Item;
import com.example.foo.product.query.ItemQuery;
import com.example.foo.product.dao.ItemDao;

@org.springframework.stereotype.Service
public class ItemService {

	private final ItemDao itemDao;

	public ItemService(ItemDao itemDao) {
		this.itemDao = itemDao;
	}

	// ========== Item ==========

	public PageResult<Item> findItems(ItemQuery itemQuery, Pageable pageable) {
		return itemDao.findItems(itemQuery, pageable);
	}

	public List<Item> findItemsByIds(List<Long> ids) {
		return itemDao.selectBatchIds(ids);
	}

	public Item findItemById(Long id) {
		return itemDao.selectById(id);
	}

	public void createItem(Item item) {
		item.setState(Item.State.unpublished);
		item.setCreatedAt(LocalDateTime.now());
		itemDao.insert(item);
	}

	public void updateItem(Item item) {
		item.setUpdatedAt(LocalDateTime.now());
		itemDao.updateById(item);
	}

	public void updateItemState(Long id, Item.State state) {
		Item item = new Item();
		item.setId(id);
		item.setState(state);
		item.setUpdatedAt(LocalDateTime.now());
		itemDao.updateById(item);
	}

}
